/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2021/4/9  lisy31662  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.service.impl;

import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.AmtAccount;
import com.sansi.pencilbook.entity.AmtJournal;
import com.sansi.pencilbook.mapper.AmtJournalMapper;
import com.sansi.pencilbook.service.AmtJournalServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * 功能说明: 资金流水表
 *
 * @author lisy31662
 */
@Service
public class AmtJournalServerImpl implements AmtJournalServer {

  @Autowired
  private AmtJournalMapper amtJournalMapper;

  @Override
  public Result getAmtJournalAll() {
    try {
      List<AmtJournal> list = amtJournalMapper.findAmtJournalAll();
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询结果为空");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getAmtJournalByUser(int userId) {
    try {
      List<AmtJournal> list = amtJournalMapper.findAmtJournalByUser(userId);
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询结果为空");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getAmtJournalByUserType(int userId, String type) {
    try {
      List<AmtJournal> list = amtJournalMapper.findAmtJournalByUserType(userId, type);
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询结果为空");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getAmtJournalByType(String type) {
    try {
      List<AmtJournal> list = amtJournalMapper.findAmtJournalByType(type);
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询结果为空");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result addAmtJournal(AmtJournal amtJournal) {
    try {
      int num = amtJournalMapper.saveAmtJournal(amtJournal);
      return num>0 ? Result.ofSuccess("添加成功", num) :
          Result.ofFailed("添加失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result takeOut(int userId, float amt, int counterparty, String time) {
    try {
      AmtJournal amtJournal = new AmtJournal();
      amtJournal.setUserId(userId);
      amtJournal.setAmt(amt);
      amtJournal.setType("0");
      amtJournal.setCounterparty(counterparty);
      amtJournal.setTime(time);
      int num = amtJournalMapper.saveAmtJournal(amtJournal);
      return num>0 ? Result.ofSuccess("插入成功", num) :
           Result.ofFailed("插入失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result takeIn(int userId, float amt, int counterparty, String time) {
    try {
      AmtJournal amtJournal = new AmtJournal();
      amtJournal.setUserId(userId);
      amtJournal.setAmt(amt);
      amtJournal.setType("1");
      amtJournal.setCounterparty(counterparty);
      amtJournal.setTime(time);
      int num = amtJournalMapper.saveAmtJournal(amtJournal);
      return num>0 ? Result.ofSuccess("插入成功", num) :
          Result.ofFailed("插入失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed(throwables.getMessage());
    }
  }
}
